$().ready(function() {
	loadwarehouse();
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/pro/instock/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}
//查询所有仓库
function loadwarehouse() {
	var html = "";
	$.ajax({
		url : '/pro/warehouse/getalllist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-warehouseId").append(html);
			$(".chosen-select-warehouseId").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-warehouseId').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}
//根据SKU查询商品基本信息
function findSku() {
	$.ajax({
		url :"/pro/product/getsku",
		data: {
			sku: function () {
				return $("#sku").val();
			}
		},
		type:"Post",
		dataType:"json",
		success : function(data) {
			// $("input[name='picurl']").val(data.picturl);
			$("input[name='skuId']").val(data.id);
			$("input[name='proName']").val(data.name);
			$("input[name='proModel']").val(data.proModel);
			$("input[name='proSpec']").val(data.proSpec);
			$("input[name='proBrand']").val(data.proBrand);
			$("input[name='picOrigin']").val(data.picOrigin);
			loadPurInfo();
		}
	});
}
//查询采购合同编号
function loadPurInfo() {
	var html = "";
	$.ajax({
		url : '/pur/group/getlistByPayment',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].groupid + '</option>'
			}
			$(".chosen-select-groupid").append(html);
			$(".chosen-select-groupid").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-groupid').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
				var url ='/pur/group/getgroup/'+params.selected;
				$.ajax({
					url :url,
					data:null,
					type:"get",
					success : function(data) {
						$("input[name='groupid']").val(data.groupid);
						$("input[name='supplierId']").val(data.supplierId);
						$("input[name='supplierName']").val(data.supplierName);
					}
				});
			});
		}
	});
}
//计算价格
function inStockQty() {
	var inqty = Number($("input[ name='inqty']").val());
	var purPrice = Number($("input[ name='purPrice']").val());
	var taxRate = Number($("input[ name='taxRate']").val());
	//不含税价
	var noTaxPrice = purPrice*(1-taxRate);
	noTaxPrice = noTaxPrice.toFixed(2);
	$("#noTaxPrice").val(noTaxPrice);
	//总价
	var allPrice = purPrice*inqty;
	allPrice = allPrice.toFixed(2);
	$("#allPrice").val(allPrice);
}
//上次入库凭证
layui.use('upload', function(){
	var upload = layui.upload;
	//执行实例
	var uploadInst = upload.render({
		elem: '#upLoad' //绑定元素
		,url: '/common/sysFile/upload' //上传接口
		,before: function(obj){
			//预读本地文件示例，不支持ie8
			obj.preview(function(index, file, result){
				$('#pic').attr('src', result); //图片链接（base64）
			});
		}
		,done: function(res){
			//上传完毕回调
			console.log(res,"fileName");
			$('#inqtyUrl').val(res.fileName)
		}
		,error: function(){
			//请求异常回调
		}
	});
})
